﻿#include <stdio.h>			//printf
#include <sys/types.h>		//open
#include <sys/stat.h>		//open
#include <fcntl.h>			//open
#include <termios.h>		//tcflush
#include <unistd.h>			//tcflush
#include <string.h>			//memset
#include <pthread.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <stdlib.h>

#define DEVICE_NAME "/dev/s3c2410_serial3"

int ser_fd = 0;
void show_dht11(void);
/*
void *dht11_thread (void *)
{	while(1)
	{
		show_dht11();
	}
	pthread_exit(NULL);
}
*/
int uart_open(void)
{
	ser_fd = open(DEVICE_NAME,O_RDWR);
	if(ser_fd == -1)
	{
		printf("serical open failed!\n");
		return -1;
	}
	set_serial_uart1(ser_fd);
}

void show_dht11(void)
{
	char message[6];
	
	memset(message,0,6);
	read(ser_fd,message,sizeof(message));
	tcflush( ser_fd, TCIOFLUSH); //刷新缓冲区
	if(strcmp(message,"quit")== 0)
		exit(1);
	printf("get cmd:%s\n", message);
		/*
		printf("温度:%c", message[1]);
		printf("%c，\t", message[2]);
		printf("湿度:%c", message[4]);
		printf("%c\n", message[5]);
		*/
	
}

void ctl_zigbee(char *cmd)
{
	
	
		write(ser_fd,cmd,strlen(cmd));
		tcflush(ser_fd, TCIOFLUSH); //刷新缓冲区
	
}

int main(int argc, char **argv)
{
	uart_open();
	//pthread_t pid;
	
/*
       int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
                          void *(*start_routine) (void *), void *arg);
*/
//	pthread_create(&pid, NULL,dht11_thread,0);
	char cmd[10]={0};
	
	while(1)
	{	
		printf("put cmd:");
		scanf("%s",cmd);
		ctl_zigbee(cmd);
		show_dht11();
	}
	//pthread_join(&pid,NULL);
	return 0;
}